package com.demo.apparel.service.web.Impl;

import com.demo.apparel.entity.WxUser;
import com.demo.apparel.mapper.web.WebWxUserMapper;
import com.demo.apparel.service.web.WebWxUserService;
import com.demo.apparel.vo.PageVO;
import com.demo.apparel.vo.web.WebWxUserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class WebWxUserServiceImpl implements WebWxUserService {

    @Autowired
    private WebWxUserMapper webWxUserMapper;

    /**
     *
     * @param pageNum
     * @param pageSize
     * @param username
     * @param phone
     * @return
     */
    public PageVO<WxUser> getUsersPage(int pageNum, int pageSize, String username, String phone) {
        int offset = (pageNum - 1) * pageSize;
        List<WxUser> users = webWxUserMapper.selectUsersPage(username, phone, pageSize, offset);
        int total = webWxUserMapper.countUsers(username, phone);
        return new PageVO<>(users, total, pageNum, pageSize);
    }


    @Override
    public List<WebWxUserVO> exportUsers() {
        List<WxUser> list = webWxUserMapper.selectAllUsers();
        return list.stream().map(user -> {
            WebWxUserVO vo = new WebWxUserVO();
            BeanUtils.copyProperties(user, vo);
            return vo;
        }).collect(Collectors.toList());
    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteUserById(String id) {
        return webWxUserMapper.deleteById(id) > 0;
    }

    /**
     *
     * @param ids
     * @return
     */
    @Override
    public boolean deleteUsersByIds(List<String> ids) {
        if (ids == null || ids.isEmpty()) {
            return false;
        }
        return webWxUserMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 获取所有的用户
     * @return
     */
    @Override
    public List<WxUser> getAllUsers() {
        return webWxUserMapper.selectAll();
    }

}
